package cn.tj212.xiaoyin.portal.business.impl;

import cn.tj212.xiaoyin.core.consts.CourseEnum;
import cn.tj212.xiaoyin.core.course.domain.CourseSection;
import cn.tj212.xiaoyin.core.course.service.ICourseSectionService;
import cn.tj212.xiaoyin.portal.business.ICourseBusiness;
import cn.tj212.xiaoyin.portal.vo.CourseSectionVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class CourseBusinessImpl implements ICourseBusiness {

    @Autowired
    private ICourseSectionService courseSectionService;

    /**
     * 获取课程章节
     * @param courseId
     * @return
     */
    @Override
    public List<CourseSectionVO> queryCourseSection(Long courseId) {
        List<CourseSectionVO> resultList=new ArrayList<CourseSectionVO>();
        CourseSection queryEntity=new CourseSection();
        queryEntity.setCourseId(courseId);
        queryEntity.setOnsale(CourseEnum.ONSALE.value());

        Map<Long,CourseSectionVO> tmpMap=new LinkedHashMap<>();
        Iterator<CourseSection> it=courseSectionService.queryAll(queryEntity).iterator();
        while (it.hasNext()){
            CourseSection item=it.next();
            if (Long.valueOf(0).equals(item.getParentId())){//章
                CourseSectionVO vo=new CourseSectionVO();
                BeanUtils.copyProperties(item,vo);
                tmpMap.put(vo.getId(),vo);
            }else {
                tmpMap.get(item.getParentId()).getSections().add(item);//小节添加到大章中
            }
        }
        for (CourseSectionVO vo:tmpMap.values()
             ) {
            resultList.add(vo);
        }
        return resultList;
    }
}
